+function ($) {
    "use strict";
    var upload, defaults = {
        imgList: [],
        url: 'https://devapi.mcloudsoft.cn/app/v1/oss/UploadLargeFile',
        edit: true,
        max: 9,
        multiple: true,
        accept: ['image/jpg', 'image/jpeg', 'image/png', 'image/gif'],
        onRemoveSuccess: undefined,
        onUploadSuccess: undefined,
        onUploadFail: undefined,
        size: 1,//M
        type: 1,//1图文 2视频
        tpl: '<div class="page-bd-15" style="margin-top:10px">\
        <div class="weui-uploader">\
            <div class="weui-uploader__bd">\
                {{if(type==1)}}\
                <ul class="weui-uploader__files">\
                {{each imgList item}}\
                <li src="{{item}}" data-item="{{item}}"  class="weui-uploader__file" style="margin-top:9px;position:relative;background-image:url({{item}})"><input value="{{item}}"  type="hidden"  name="files" />\
                {{if(edit)}}\
                <span  style="position: absolute;top: -.7em;right:0em;"><i style="color:#FA5151;font-size: 22px;" class="weui-icon-clear"></i></span>\
                {{/if}}\
                </li>\
                {{/each}}\
                </ul>\
                {{else}}\
                <div id="videodiv" style="position:relative;margin-top:10px;">\
                {{each imgList item}}\
                <video width="100%" height="300px" src="{{item}}"></video>\
                {{/each}}\
                </div>\
                {{/if}}\
                {{if(edit)}}\
                <div style="margin-top:10px;margin-left:10px;" class="weui-uploader__input-box">\
                {{if(type==1)}}\
                <input class="weui-uploader__input" accept="image/*" multiple="multiple" type="file">\
                {{else}}\
                <input class="weui-uploader__input" accept="video/*" type="file">\
                {{/if}}\
                </div>\
                {{/if}}\
            </div>\
        </div>',
        readonlytpl: ' <div class="page-bd-15">\
        <div class="weui-uploader">\
            <div class="weui-uploader__bd">\
                <ul class="weui-uploader__files">\
                {{#imgList}}\
                <li src="{{this}}" data-item="{{this}}"  class="weui-uploader__file" style="margin-top:9px;position:relative;background-image:url({{this}})"><input value="{{this}}"  type="hidden"  name="files" />\
                </li>\
                {{/imgList}}\
                </ul>\
            </div>\
        </div>',
    }
    function ajaxupload(params, file) {
        return new Promise((resolve, reject) => {
            if (params.type == 2) {
                // 获取上传的视频的宽高
                //视频
                const url = URL.createObjectURL(file)
                const video = document.createElement('video')
                video.src = url;
                video.onloadedmetadata = () => {
                    URL.revokeObjectURL(url)
                    console.log(video.videoWidth, video.videoHeight, '----video');
                    //分辨率 200 100
                    const resolvingPower = video.videoWidth / video.videoHeight;
                    alert(resolvingPower);
                    var videoWidth = 100;
                    var videoHeight = videoWidth / resolvingPower;
                    if (videoWidth < videoHeight) {//竖屏
                        videoWidth = 100;
                        videoHeight = videoWidth / resolvingPower;
                    } else {//横屏
                        videoWidth = 250;
                        videoHeight = videoWidth / resolvingPower;
                    }
                }
            }
            var formData = new FormData();
            formData.append("userfile", file);
            $.ajax({
                type: 'POST',
                url: params.url,
                data: formData,
                cache: false,
                processData: false,
                contentType: false,
                success: function (res) {
                    var url = res.data.url;
                    if (params.type == 1) {
                        upload.find("ul").append(' <li src="' + url + '" data-item="' + url +
                            '" class="weui-uploader__file" style="margin-top:9px;position:relative;background-image:url('
                            + url + ')"><input value=' + url + ' type="hidden"  name="files" />\
                                        <span style="position: absolute;top:-.7em;right:0em;"><i style="color:#FA5151;font-size: 22px;" class="weui-icon-clear"></i></span>\
                                        </li>');
                        if (params.max <= upload.find("li").length) {
                            $(".weui-uploader__input-box").hide();
                        }
                    } else {
                        upload.find("#videodiv").append('<video controls width="100%" height="300" src="' + url
                            + '"></video><span data-item="' + url + '"  style="position: absolute;top: -.7em;right:0em;"><i style="color:#FA5151;font-size: 22px;" class="weui-icon-clear"></i></span>');
                        $(".weui-uploader__input-box").hide();
                    }
                    params.onUploadSuccess && typeof params.onUploadSuccess == "function" && params.onUploadSuccess(res.data.url);
                    resolve(url);
                }, fail: function (res) {
                    params.onUploadFail && typeof params.onUploadFail == "function" && params.onUploadFail(res);
                    reject(res);
                }
            });
        })
    }
    $.fn.lobsterupload = function (params) {
        return this.each(function () {
            params = $.extend({}, defaults, params);
            if (params.max <= params.imgList.length) {
                $(".weui-uploader__input-box").hide();
            }
            upload = $(this);
            upload.html(template.render(params.tpl, params));
            var promisearr = [];
            upload.on("change", "[type='file']", function (event) {
                // $.showLoading("上传中");
                var files = event.target.files;
                // 如果没有选中文件，直接返回
                if (files.length === 0) {
                    return;
                }
                if (files.length > params.max) {
                    $.toast("上传数量不能超过" + params.max + "张", 'forbidden');
                    return;
                }
                var flag = true;
                for (var i = 0; i < files.length; i++) {
                    var file = files[i];
                    if (params.type == 1) {
                        if (file.size > params.size * 1024 * 1024) {
                            $.toast('图片超过' + params.size + 'M，不允许上传', 'forbidden');
                            flag = false;
                            break;
                        }
                    } else {
                        if (file.size > params.size * 1024 * 1024) {
                            $.toast('视频超过' + params.size + 'M，不允许上传', 'forbidden');
                            flag = false;
                            break;
                        }
                    }
                    promisearr.push(ajaxupload(params, file));
                }
                if (!flag)
                    return;
                $.showLoading("正在上传")
                Promise.all(promisearr).then(res => {
                    console.log(res);
                    $.hideLoading();
                }).catch(res => {
                    $.hideLoading();
                    console.log(res.msg || res);
                    $.toast(res.msg || res, "forbidden");
                })
            });
            upload.on("click", "span", function (event) {
                event.stopPropagation();
                var _this = $(this);
                $.confirm('您确定要删除吗?', '确认删除?', function () {
                    if (params.type == 1) {
                        var obj = _this.parent();
                        var item = obj.data("item");
                        $(obj).remove();
                        if (upload.find("li").length >= params.max) {
                            $(".weui-uploader__input-box").hide();
                        } else {
                            $(".weui-uploader__input-box").show();
                        }
                        params.onRemoveSuccess && typeof params.onRemoveSuccess == "function" && params.onRemoveSuccess(item);
                    } else {
                        var obj = _this.parent();
                        var item = _this.data("item");
                        obj.html("");
                        $(".weui-uploader__input-box").show();
                        params.onRemoveSuccess && typeof params.onRemoveSuccess == "function" && params.onRemoveSuccess(item);
                    }
                });
                return false;
            })
        })
    };
}($);